﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SandThiefWeb.Helper;
using Swashbuckle.AspNetCore.Swagger;

namespace SandThiefWeb
{
    public class Startup
    {
        private static readonly string swaggerDocName = "v1";
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            //添加cors 服务 配置跨域处理            
            services.AddCors(options =>
            {
                options.AddPolicy("any", builder =>
                {
                    builder.AllowAnyOrigin() //允许任何来源的主机访问
                    .AllowAnyMethod()
                    .AllowAnyHeader();

                });
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            //数据库连接字符串
            services.AddDbContext<SandThiefDal.SandThiefDbContext>(opt => opt.UseMySQL(Configuration.GetConnectionString("MySql")));

            services.AddScoped<SandThiefIBll.Ist_user, SandThiefBll.st_userBllL>();



            services.Configure<RedisConn>(this.Configuration.GetSection("Cache"));
            
            services.AddSwaggerGen(c =>
            {
                c.IgnoreObsoleteActions();
                c.SwaggerDoc(
                    // name: 
                    name: swaggerDocName,
                    // info: 
                    info: new Info
                    {
                        Title = "SandThiefWeb",
                        Version = "1.0.0",
                        Description = "接口api示例",
                        //TermsOfService = "None",
                        //Contact = new Contact { Name = "Jayson Xu", Url = "wiseant@163.com" }
                    }
                );

                var security = new Dictionary<string, IEnumerable<string>> { { "Bearer", new string[] { } } };
                c.AddSecurityRequirement(security);
                c.AddSecurityDefinition("Bearer", new ApiKeyScheme
                {
                    Description = "权限认证(数据将在请求头中进行传输) 参数结构: \"Authorization: Bearer {token}\"",
                    Name = "Authorization",//jwt默认的参数名称
                    In = "header",//jwt默认存放Authorization信息的位置(请求头中)
                    Type = "apiKey"
                });//Authorization的设置
                //应用XML注释文档
                var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//获取应用程序所在目录
                var xmlPath = Path.Combine(basePath, "SandThiefWeb.xml");
                c.IncludeXmlComments(xmlPath);
            });


        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseCors("any");
            app.UseCookiePolicy();
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint(
                    // url: 需配合 SwaggerDoc 的 name。 "/swagger/{SwaggerDoc name}/swagger.json"
                    url: $"../swagger/{swaggerDocName}/swagger.json",

                    name: "SandThiefWeb API v1.0.0"
                );
            });


            app.UseStaticFiles(new StaticFileOptions()
            {
                FileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()),
                ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string>
                {
                      { ".apk", "application/vnd.android.package-archive" },
                       {".3gp",    "video/3gpp"},
                        {".asf",    "video/x-ms-asf"},
                        {".avi",    "video/x-msvideo"},
                        {".bin",    "application/octet-stream"},
                        {".bmp",    "image/bmp"},
                        {".c",  "text/plain"},
                        {".class",  "application/octet-stream"},
                        {".conf",   "text/plain"},
                        {".cpp",    "text/plain"},
                        {".doc",    "application/msword"},
                        {".docx",   "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
                        {".xls",    "application/vnd.ms-excel"},
                        {".xlsx",   "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
                        {".exe",    "application/octet-stream"},
                        {".gif",    "image/gif"},
                        {".gtar",   "application/x-gtar"},
                        {".gz", "application/x-gzip"},
                        {".h",  "text/plain"},
                        {".htm",    "text/html"},
                        {".html",   "text/html"},
                        {".jar",    "application/java-archive"},
                        {".java",   "text/plain"},
                        {".jpeg",   "image/jpeg"},
                        {".jpg",    "image/jpeg"},
                        {".js", "application/x-javascript"},
                        {".log",    "text/plain"},
                        {".m3u",    "audio/x-mpegurl"},
                        {".m4a",    "audio/mp4a-latm"},
                        {".m4b",    "audio/mp4a-latm"},
                        {".m4p",    "audio/mp4a-latm"},
                        {".m4u",    "video/vnd.mpegurl"},
                        {".m4v",    "video/x-m4v"},
                        {".mov",    "video/quicktime"},
                        {".mp2",    "audio/x-mpeg"},
                        {".mp3",    "audio/x-mpeg"},
                        {".mp4",    "video/mp4"},
                        {".mpc",    "application/vnd.mpohun.certificate"},
                        {".mpe",    "video/mpeg"},
                        {".mpeg",   "video/mpeg"},
                        {".mpg",    "video/mpeg"},
                        {".mpg4",   "video/mp4"},
                        {".mpga",   "audio/mpeg"},
                        {".msg",    "application/vnd.ms-outlook"},
                        {".ogg",    "audio/ogg"},
                        {".pdf",    "application/pdf"},
                        {".png",    "image/png"},
                        {".pps",    "application/vnd.ms-powerpoint"},
                        {".ppt",    "application/vnd.ms-powerpoint"},
                        {".pptx",   "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
                        {".prop",   "text/plain"},
                        {".rc", "text/plain"},
                        {".rmvb", "audio/x-pn-realaudio"},
                        {".rtf",    "application/rtf"},
                        {".sh", "text/plain"},
                        {".tar",    "application/x-tar"},
                        {".tgz",    "application/x-compressed"},
                        {".txt",    "text/plain"},
                        {".wav",    "audio/x-wav"},
                        {".wma",    "audio/x-ms-wma"},
                        {".wmv",    "audio/x-ms-wmv"},
                        {".wps",    "application/vnd.ms-works"},
                        {".xml",    "text/plain"},
                        {".z",  "application/x-compress"},
                        {".zip",    "application/x-zip-compressed"},
                        {"",        "*/*"}

                })

            });

            app.UseHttpsRedirection();
            app.UseMvc();
        }
    }
}
